package com.example.app.adapter.catalog;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.app.R;
import com.example.app.viewmodel.catalog.CategoryList;

import java.util.List;

public class CategoryListAdapter extends BaseAdapter {
    private Context ctx;
    private List<CategoryList> categoryLists;

    public CategoryListAdapter(Context ctx, List<CategoryList> categoryLists) {
        this.ctx = ctx;
        this.categoryLists = categoryLists;
    }

    @Override
    public int getCount() {
        return this.categoryLists.size();
    }

    @Override
    public Object getItem(int position) {
        return this.categoryLists.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = new ViewHolder();
        if (convertView == null) {
            convertView = LayoutInflater.from(this.ctx).inflate(R.layout.adapter_catalog_categorylist,null);
            viewHolder.lineView = convertView.findViewById(R.id.tv_catalog_line);
            viewHolder.titleView = convertView.findViewById(R.id.tv_catalog_text);

            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (this.categoryLists.get(position).getChecked()) {
            viewHolder.lineView.setVisibility(View.VISIBLE);
        }else {
            viewHolder.lineView.setVisibility(View.INVISIBLE);
        }

        // 设置分类标题
        viewHolder.titleView.setText(this.categoryLists.get(position).getName());

        return convertView;
    }

    static class ViewHolder{
        TextView lineView;
        TextView titleView;
    }
}
